<template>
  <div class="home">
    <div class="top"><h3>地址列表</h3></div>
    <div class="main">
      <div class="list" v-if="list.length">
        <div class="item" v-for="(item,index) in list" :key="index">
          <div class="info">
            <p>{{item.name}}     {{item.tel}}</p>
      
            <p>{{item.addr}}</p>
          </div>
          <div class="emit">
            <button @click="edit(item.id)">编辑</button>
            <button @click="deletes(item.id,index)">删除</button>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom" @click="add">
      + 添加地址
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  async mounted () {
    let {id} = this.$route.query;
    let res = await axios.get(`/api/list?id=${id}`);
    this.list = res.data.data;
    console.log(this.list)
  },
  data () {
    return {
      list:[]
    }
  },
  methods: {
    add(){
      let {id} = this.$route.query;
      this.$router.push(`/index/add?id=${id}`)
    },
    edit(id){
      // let {id} = this.$route.query;
      this.$router.push(`/index/edit?id=${id}`)
    },
    async deletes(id,index){
      this.list.splice(index,1);
      await axios.get(`/api/delete?id=${id}`);   
    }
  }
}
</script>
<style lang="scss">
.home{
  width: 100%;
  height: 100%;
  .top{
    width: 100%;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: cornflowerblue;
  }
  .main{
    width: 100%;
    height: calc(100% - 94px);
    .list{
      width: 100%;
      height: 100%;
      .item{
        width: 100%;
        height: 100px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        button{
          margin: 0 5px;
          width: 70px;
          height: 30px;
          background-color: dodgerblue;
          outline: none;
          border: none;
          border-radius: 10px;
        }
        p{
          margin: 5px 0;
        }
      }
    }
  }
  .bottom{
    width: 100%;
    height: 50px;
    background: cornflowerblue;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
